%%% Returns the approximate pixel at the center of the fovea.
%%% Emily King
%%% Last updated: 27 July 2010

% Requires the multilevel Ostu thresholder found at
% http://www.biomecardio.com/matlab/otsu.html (or any other multi-level
% Ostu thresholder with syntax otsu(image, number levels)

% Input a retinal image which is somewhat centered about the fovea.

% This function is similar to cent_fov, but it crops before doing
% otsu_thresholding.

% Use a blue emission autofluorescence image.

function [r_center, c_center] = box_cent_fov(ret_image)

ret_image = im2double(ret_image); % perhaps not nec?

[rows cols] = size(ret_image);

%crop_image = ret_image(floor(2/6*rows):floor(4/6*rows),floor(2/6*cols):floor(4/6*cols));
crop_image = ret_image(floor(2.5/6*rows):floor(3.5/6*rows),floor(2.5/6*cols):floor(3.5/6*cols));

otsu_crop = otsu(crop_image, 15); %Much lower numbers work for healthier eyes.
%figure, imagesc(otsu_crop), colormap gray;

fovea = find(otsu_crop==0);

[r c] = ind2sub([size(otsu_crop)],fovea);

%x_center = round(mean(x))+floor(2/6*cols);
r_center = round(mean(r))+floor(2.5/6*rows);

%y_center = round(mean(y))+floor(2/6*rows);
c_center = round(mean(c))+floor(2.5/6*cols);

temp = ret_image; 
temp(r_center, c_center) = 1;
figure, imagesc(temp), colormap jet;

end



